package com.newtype.meeting.client.widget;

import java.util.Date;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.BeanModelLookup;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.FileUploadField;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.Radio;
import com.extjs.gxt.ui.client.widget.form.RadioGroup;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Image;
import com.newtype.meeting.client.AppEvents;
import com.newtype.meeting.client.widget.resources.Resources;
import com.newtype.meeting.shared.AccountDTO;
import com.newtype.meeting.shared.LoginInfo;

/**
 * 帐号申请窗口
 * @author 汪恭泽
 * Jun 10, 2011 11:32:40 AM
 */
public class RegistrationWindow extends Window {
	private BeanModel accountBeanModel;
	private Resources resources = GWT.create(Resources.class);

	private RadioGroup gender;
	private RadioGroup openTAG;

	public RegistrationWindow(LoginInfo loginInfo) {
		setHeading("帐号申请资料表");
		setSize(550, 650);
		setIconStyle("icon-form");
		setModal(true);
		setMaximizable(true);
		setLayout(new FormLayout());
		setScrollMode(Scroll.AUTO);

		FormData formData = new FormData("100%");

		FormPanel formPanel = new FormPanel();
		formPanel.setHeaderVisible(false);
		formPanel.setBodyBorder(false);
		formPanel.setFrame(true);
		formPanel.setButtonAlign(HorizontalAlignment.CENTER);

		Image imgLogo = new Image(resources.logo());
		formPanel.add(imgLogo);

		//姓名
		TextField<String> fullNameText = new TextField<String>();
		fullNameText.setFieldLabel("姓名");
		fullNameText.setName("fullName");
		formPanel.add(fullNameText);

		//帐号
		final TextField<String> accountIDText = new TextField<String>();
		accountIDText.setFieldLabel("帐号");
		accountIDText.setName("accountID");
		accountIDText.setAllowBlank(false);
		accountIDText.setValue(loginInfo.getNickname());
		formPanel.add(accountIDText);

		//密码
		TextField<String> passwordText = new TextField<String>();
		passwordText.setFieldLabel("密码");
		passwordText.setName("password");
		formPanel.add(passwordText);

		//确认密码
		TextField<String> confirmPasswordText = new TextField<String>();
		confirmPasswordText.setFieldLabel("再次确认密码");
		formPanel.add(confirmPasswordText);

		//性别
		Radio male = new Radio();
		male.setBoxLabel("男");
		male.setValueAttribute("1");

		Radio radio2 = new Radio();
		radio2.setBoxLabel("女");
		radio2.setValueAttribute("0");

		gender = new RadioGroup("gender");
		gender.setName("gender");
		gender.setFieldLabel("性别");
		gender.add(male);
		gender.add(radio2);
		gender.setValue(male);
		formPanel.add(gender);

		DateField birthday = new DateField();
		birthday.setFieldLabel("生日");
		birthday.setName("birthday");
		formPanel.add(birthday);

		TextField<String> officePhoneNO = new TextField<String>();
		officePhoneNO.setFieldLabel("公司电话");
		officePhoneNO.setName("officePhoneNO");
		formPanel.add(officePhoneNO);

		TextField<String> officephoneEXT = new TextField<String>();
		officephoneEXT.setFieldLabel("分机");
		officephoneEXT.setName("officephoneEXT");
		formPanel.add(officephoneEXT);

		TextField<String> email = new TextField<String>();
		email.setFieldLabel("Email");
		email.setName("email");
		email.setValue(loginInfo.getEmailAddress());
		formPanel.add(email, formData);

		TextField<String> msn = new TextField<String>();
		msn.setFieldLabel("MSN");
		msn.setName("msn");
		formPanel.add(msn, formData);

		TextField<String> skype = new TextField<String>();
		skype.setFieldLabel("Skype");
		skype.setName("skype");
		formPanel.add(skype, formData);

		TextField<String> phone = new TextField<String>();
		phone.setFieldLabel("手机");
		phone.setName("phone");
		formPanel.add(phone, formData);

		TextField<String> pager = new TextField<String>();
		pager.setFieldLabel("呼叫器");
		pager.setName("pager");
		formPanel.add(pager, formData);

		TextField<String> homePhoneNO = new TextField<String>();
		homePhoneNO.setFieldLabel("家用电话");
		homePhoneNO.setName("homePhoneNO");
		formPanel.add(homePhoneNO, formData);

		TextField<String> emergencyContact = new TextField<String>();
		emergencyContact.setFieldLabel("紧急联络人");
		emergencyContact.setName("emergencyContact");
		formPanel.add(emergencyContact, formData);

		TextField<String> emergencyPhoneNO = new TextField<String>();
		emergencyPhoneNO.setFieldLabel("紧急联络电话");
		emergencyPhoneNO.setName("emergencyPhoneNO");
		formPanel.add(emergencyPhoneNO, formData);

		TextArea address = new TextArea();
		address.setFieldLabel("通信地址");
		address.setName("address");
		formPanel.add(address, formData);

		TextArea introduces = new TextArea();
		introduces.setFieldLabel("个人简介");
		introduces.setName("introduces");
		formPanel.add(introduces, formData);

		FileUploadField personIMG = new FileUploadField();
		personIMG.setFieldLabel("个人照片");
		personIMG.setName("personIMG");
		formPanel.add(personIMG, formData);

		Radio radio3 = new Radio();
		radio3.setBoxLabel("是");
		radio3.setValueAttribute("1");

		Radio radio4 = new Radio();
		radio4.setBoxLabel("否");
		radio4.setValueAttribute("0");

		openTAG = new RadioGroup("openTAG");
		openTAG.setFieldLabel("公开个人资料");
		openTAG.add(radio3);
		openTAG.add(radio4);
		openTAG.setName("openTAG");
		openTAG.setValue(radio3);
		formPanel.add(openTAG, formData);

		AccountDTO accountDTO = new AccountDTO();
		accountDTO.setAccountID(loginInfo.getNickname());
		accountDTO.setEmail(loginInfo.getEmailAddress());
		accountDTO.setBirthday(new Date());

		accountBeanModel = BeanModelLookup.get().getFactory(AccountDTO.class)
				.createModel(accountDTO);

		//绑定数据
		FormBinding binding = new FormBinding(formPanel, true);
		binding.bind(accountBeanModel);

		formPanel.addButton(getSaveButton());
		add(formPanel);
	}

	private Button getSaveButton() {
		Button button = new Button("送出");
		button.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				AccountDTO accountDTO = accountBeanModel.getBean();
				accountDTO.setGender(gender.getValue().getValueAttribute());
				accountDTO.setOpenTAG(openTAG.getValue().getValueAttribute());
				Dispatcher.forwardEvent(AppEvents.RegisterAccount, accountDTO);
				hide();
			}
		});
		return button;
	}

}
